package com.strp.mptcp.common;

import lombok.Data;

/**
 * @author 曹行阳
 * @version 0.1
 * @date 2025/6/12 11:35
 */
@Data
public class NetworkStats {
    private double uploadPercentage;
    private long totalBandwidthBps; // 总带宽 (bits per second)
    private long averageRttMs;      // 平均端到端时延 (ms)
    private double lossRatePercent; // 丢包率 (%)

    private long totalBytesSent = 0;
    private long totalBytesReceived = 0;
    private long totalRttMs = 0;
    private int totalRttSamples = 0;
    private int totalPacketsSent = 0;
    private int totalPacketsReceived = 0;
    private int totalPacketsLost = 0;

    // Log bytes sent and update stats
    public void logBytesSent(int bytes) {
        totalBytesSent += bytes;
        totalPacketsSent++;
        // Update bandwidth
        updateBandwidth();
        System.out.println("Bytes sent: " + bytes);
    }

    // Log bytes received and update stats
    public void logBytesReceived(int bytes) {
        totalBytesReceived += bytes;
        totalPacketsReceived++;
        // Update loss rate
        updateLossRate();
        System.out.println("Bytes received: " + bytes);
    }

    // Log RTT sample and update average RTT
    public void logRttSample(long rttMs) {
        totalRttMs += rttMs;
        totalRttSamples++;
        updateAverageRtt();
        System.out.println("RTT sample logged: " + rttMs + "ms");
    }

    // Log packet loss and update loss rate
    public void logPacketLoss() {
        totalPacketsLost++;
        updateLossRate();
        System.out.println("Packet lost");
    }

    // Calculate and update the bandwidth (in bits per second)
    private void updateBandwidth() {
        // Assuming a fixed time interval (e.g., 1 second) between logs:
        long elapsedTimeSec = 1;  // Replace with actual time difference calculation
        long totalBytes = totalBytesSent + totalBytesReceived;
        totalBandwidthBps = (totalBytes * 8) / elapsedTimeSec; // Convert bytes to bits
    }

    // Update the average RTT
    private void updateAverageRtt() {
        if (totalRttSamples > 0) {
            averageRttMs = totalRttMs / totalRttSamples;
        }
    }

    // Calculate and update packet loss rate
    private void updateLossRate() {
        if (totalPacketsSent > 0) {
            lossRatePercent = (double) totalPacketsLost / totalPacketsSent * 100;
        }
    }

    // Calculate the upload percentage
    public void updateUploadPercentage() {
        long totalData = totalBytesSent + totalBytesReceived;
        if (totalData > 0) {
            uploadPercentage = (double) totalBytesSent / totalData * 100;
        }
    }

    // Reset all statistics (useful for new time intervals)
    public void reset() {
        totalBytesSent = 0;
        totalBytesReceived = 0;
        totalRttMs = 0;
        totalRttSamples = 0;
        totalPacketsSent = 0;
        totalPacketsReceived = 0;
        totalPacketsLost = 0;
        uploadPercentage = 0;
        totalBandwidthBps = 0;
        averageRttMs = 0;
        lossRatePercent = 0;
    }
}

